import router from './router';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import { getAccessToken } from '@/utils/auth';
import store from './store'

//第三方白名单
const whiteList = ['/login'];

router.beforeEach((to,from,next)=>{
    NProgress.start();
    if(getAccessToken()) {
        if(to.path === '/login') {
            next()
            NProgress.done();
            return;
        }else {
            store.dispatch('generateRoutes').then(accessRoutes => {
                const has = router.getRoutes().length > 14;
                if(has) {
                    next();
                }else {
                    router.addRoutes(accessRoutes)
                    next({...to, replace:true})
                }
                console.log('AAAAAAAAAAAA',router.getRoutes())
            })
        }
        document.title = to.name + '-Fizz网关管理系统'
    }else {
        // 没有token
        if(whiteList.indexOf(to.path) !== -1) {
            next();
        }else {
            next('/login')
            NProgress.done();
        }
    }
})

router.afterEach(()=>{
    NProgress.done();
})